package assignments.sagarjoshi.springtms;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Calendar;


public class Activity_Allocation extends Activity implements AllocationListFragment.OnAllocationInteractionListener, TMSDatePickerFragment.OnTMSDateChangedListener
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_allocation);
        String date=(new SimpleDateFormat("dd/MM/yyyy")).format(Calendar.getInstance().getTime());
        ((Button)findViewById(R.id.lbl_coursedate)).setText(date);
        recreatelist();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_admin_home, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement

            if (id == R.id.action_logout)
            {
                startActivity(new Intent(this,LoginScreen.class));
                this.finish();
                return true;
            }



        return super.onOptionsItemSelected(item);
    }

    private void recreatelist()
    {
        Bundle b= new Bundle();
        AllocationListFragment frag = new AllocationListFragment();
        frag.setArguments(b);
        getFragmentManager().beginTransaction().replace(R.id.alloclistframe,frag).commit();
    }

    public void onAllocationChanged(int data)
    {

    }

    @Override
    public void setCounts(int count1, int count2, int count3, int count4)
    {
        ((TextView)findViewById(R.id.val_count1)).setText(String.valueOf(count1));
        ((TextView)findViewById(R.id.val_count2)).setText(String.valueOf(count2));
        ((TextView)findViewById(R.id.val_count3)).setText(String.valueOf(count3));
        ((TextView)findViewById(R.id.val_count4)).setText(String.valueOf(count4));
    }


    public void onButtonClicked(View v)
    {
        int id=v.getId();

        if(id==R.id.lbl_coursedate)
        {
            new TMSDatePickerFragment().show(getFragmentManager(),"TMSDATEPICKER");
        }

        if(id==R.id.but_editcourse)
        {
            int item_id=getIntent().getIntExtra("item_id",0);
            if(item_id==0) return;
            Intent i = new Intent(this,AddorEditCourse.class);
            i.putExtra("item_id",item_id);
            startActivity(i);

        }

        if(id==R.id.but_save)
        {
            ((AllocationListFragment)getFragmentManager().findFragmentById(R.id.alloclistframe)).onSave();
        }
    }

    @Override
    public void onTMSDateChanged(int year, int monthOfYear, int dayOfMonth)
    {
        String month=String.valueOf(monthOfYear);
        String day = String.valueOf(dayOfMonth);
        if(monthOfYear<10)
            month="0"+month;
        if(dayOfMonth<10)
            day="0"+day;
        ((Button)findViewById(R.id.lbl_coursedate)).setText(day+"/"+month+"/"+year);
    }
}
